package com.learning.mymedia;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.documentfile.provider.DocumentFile;

public class VideoTest extends AppCompatActivity implements View.OnClickListener{
    private VideoView videoView;
    private ActivityResultLauncher<Intent> selectVideoFileLauncher;
    private Uri videoUri;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_video_test);
        videoView = findViewById(R.id.vv_content);
        findViewById(R.id.btn_video_choose).setOnClickListener(this);

        // 初始化 ActivityResultLauncher
        selectVideoFileLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null && data.getData() != null) {
                            videoUri = data.getData();
                            videoView.setVideoURI(videoUri);
                            MediaController mc = new MediaController(this);
                            videoView.setMediaController(mc);
                            mc.setMediaPlayer(videoView);
                            videoView.start();
                        }
                    }
                }
        );



    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_video_choose) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("video/*");
            selectVideoFileLauncher.launch(intent);
        }
    }
}